﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Dev.Control;
using TT.Module.sysLogin;
using TT.ORM;
using Exp.BillDLL.biz;
using Bill.BasDLL.ety;


namespace ExpBillMain.form
{
    public partial class frmArea : Dev.Control.BaseFormDev
    {
        bizArea bzArea;
        etyBaeSystemParamter _basSysParam;
        IList<etyBaseTable> ilSF;
        IList<etyArea> ilAreaCurr = new List<etyArea>();
        bool isFirstLoad = true;

        public frmArea(etyBaeSystemParamter ibasSysParam, 
            bool isViewToolBar, 
            IList< TT.ORM.etyCodeRef> ilBussType,
            IList<etyBaseTable> _ilSF
            )
        {
            InitializeComponent();
            _basSysParam = ibasSysParam;
            if (isViewToolBar == false)
            {
                toolStrip1.Visible = false;
                this.ggcDocList.Location = new System.Drawing.Point(0, ggcDocList.Location.Y-toolStrip1.Height);
                ggcDocList.Height += toolStrip1.Height;
                cPanelControl2.Location = new System.Drawing.Point(0, cPanelControl2.Location.Y - toolStrip1.Height);
            }

            ilSF = _ilSF;
            //业务类型
            string sMapingCols = "CODE_DESC";
            string sMapingHeaderTxt = "业务类型";
            string sColumnWidth = "20";
            cboBussType.ColumnsMappingCreate(sMapingCols, sMapingHeaderTxt, sColumnWidth);
            cboBussType.DataSource = ilBussType;

        }

        private void frmArea_Load(object sender, EventArgs e)
        {
            bzArea = new bizArea(_basSysParam);

            //客户下拉
            string sMapingCols = "CLIENT_NAME";
            string sMapingHeaderTxt = "客户名称";
            string sColumnWidth = "30";
            cboClient.ColumnsMappingCreate(sMapingCols, sMapingHeaderTxt, sColumnWidth);
            cboClient.DisplayMember = "CLIENT_NAME";
            cboClient.KeyPressSeekMember = "CLIENT_NAME";
            cboClient.ValueMember = "CLIENT_ID";
            cboClient.DataSource = _Global.ilClient;
            //this.cboClient.Properties.TextEditStyle = DevExpress.XtraEditors.Controls.TextEditStyles.Standard;

            
            //客户代码
            ggcDocList.GridView = this.gridView1;
            sMapingCols = "CLIENT_NAME,AREA_NAME,BUSS_DESC,SF_DESC";
            sMapingHeaderTxt = "客户名,区域,业务类型,省份";
            ggcDocList.ColumnsMappingCreate(sMapingCols, sMapingHeaderTxt);
            this.winUtil.LoadGridLayout(ggcDocList);

            cboClient.ItemIndex = 0;
            cboBussType.ItemIndex = 1;
            ggcDocList.GridView.OptionsView.ShowGroupPanel = true;

        }

        private void cboClient_EditValueChanged(object sender, EventArgs e)
        {
            RefreshData();
        }

        private void cboBussType_EditValueChanged(object sender, EventArgs e)
        {
            RefreshData();
        }

        //下拉切换调用
        private void RefreshData()
        {
            tvArea.SelectedNode = null;
            if (cboClient.Text.Trim() == "") return;
            if (cboBussType.Text.Trim() == "") return;
            etyArea eyEty = new etyArea();
            eyEty.CLIENT_ID = cboClient.Properties.KeyValue.ToString();
            eyEty.BUSS_TYPE = cboBussType.Properties.KeyValue.ToString();
            ilAreaCurr = bzArea.GetArea(eyEty);
            ggcDocList.DataSource = ilAreaCurr;
            if (ilAreaCurr.Count > 0)
            {
                #region 填充区TreeList
                tvArea.Nodes.Clear();
                IEnumerable<IGrouping<string, etyArea>> ilGroup = ilAreaCurr.GroupBy(pallet => pallet.AREA_NAME);
                foreach (IGrouping<string, etyArea> grp in ilGroup)
                {
                    TreeNode tn = new TreeNode();
                    tn.Name = grp.Key.ToString();
                    tn.Text = tn.Name;
                    tvArea.Nodes.Add(tn);
                }
                #endregion
            }
            else
            {
                //tvSF_NotSelect.Nodes.Clear();
                //tvSF_NotSelect.NodeText = "X_CNAME";
                //tvSF_NotSelect.DataSource = ilSF;
                tvSF_NotSelect.Nodes.Clear();
                tvArea.Nodes.Clear();
                tvSF_Selected.Nodes.Clear();
            }

            if (isFirstLoad)
            {
                foreach (TreeNode tn in tvArea.Nodes)
                {
                    if (tn.Text == "二区")
                        tvArea.SelectedNode = tn;
                }
                isFirstLoad = false;
            }
        }

        public override etyQuickKey NewData()
        {
            frmAreaAdd fmAreaAdd = new frmAreaAdd();
            fmAreaAdd.ShowDialog();
            if (fmAreaAdd.sAreaName.Trim() != "")
            {
                tvArea.Nodes.Add(fmAreaAdd.sAreaName);
            }
            return base.NewData();
        }

        private void tsbStart_Click(object sender, EventArgs e)
        {
            NewData();
        }

        private void tvArea_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (ilAreaCurr.Count > 0)
            {
                //填充已纳入树
                IList<etyBaseTable> ilAreaSelected = new List<etyBaseTable>();
                etyBaseTable eyBasTbl = new etyBaseTable();
                string sArea = tvArea.SelectedNode.Text;
                foreach (etyArea eyArea in ilAreaCurr)
                {
                    if (eyArea.AREA_NAME == sArea)
                    {
                        eyBasTbl = new etyBaseTable();
                        eyBasTbl.X_CODE = eyArea.SF;
                        eyBasTbl.X_CNAME = eyArea.SF_DESC;
                        ilAreaSelected.Add(eyBasTbl);
                    }
                }
                tvSF_Selected.NodeText = "X_CNAME";
                tvSF_Selected.DataSource = ilAreaSelected;

                #region 显示未纳入区
                bool isHave = false;
                IList<etyBaseTable> ilNotSelectSF = new List<etyBaseTable>();
                foreach (etyBaseTable eyBas in ilSF)
                {
                    isHave = false;
                    foreach (etyArea eyArea in ilAreaCurr)
                    {
                        if (eyBas.X_CNAME == eyArea.SF_DESC)
                        {
                            isHave = true;
                            break;
                        }
                    }
                    if (isHave == false)
                    {
                        ilNotSelectSF.Add(eyBas);
                    }
                }
                tvSF_NotSelect.NodeText = "X_CNAME";
                tvSF_NotSelect.DataSource = ilNotSelectSF;
                #endregion
            }
            else
            {
                tvSF_NotSelect.Nodes.Clear();
                tvSF_NotSelect.NodeText = "X_CNAME";
                tvSF_NotSelect.DataSource = ilSF;
                tvSF_Selected.Nodes.Clear();
            }
        }

        private void tvSF_Selected_DoubleClick(object sender, EventArgs e)
        {
            Left_2_Right();
        }

        private void cbo2Right_Click(object sender, EventArgs e)
        {
            Left_2_Right();
        }

        private void Left_2_Right()
        {
            TreeNode tn = new TreeNode();
            TreeNode tnSelect = tvSF_Selected.SelectedNode;
            tn.Text = tnSelect.Text;
            tn.Tag = tnSelect.Tag;
            tvSF_NotSelect.Nodes.Add(tn);

            tvSF_Selected.SelectedNode.Remove();

            SaveOrDeleteArea(tn, false);
        }

        private void tvSF_NotSelect_DoubleClick(object sender, EventArgs e)
        {
            Right_2_Left();
        }

        private void cbo2Left_Click(object sender, EventArgs e)
        {
            Right_2_Left();
        }

        private void Right_2_Left()
        {
            TreeNode tn = new TreeNode();
            TreeNode tnSelect = tvSF_NotSelect.SelectedNode;
            tn.Text = tnSelect.Text;
            tn.Tag = tnSelect.Tag;
            tvSF_Selected.Nodes.Add(tn);

            tvSF_NotSelect.SelectedNode.Remove();

            SaveOrDeleteArea(tn,true);
        }

        private void SaveOrDeleteArea(TreeNode tn,bool isInsert)
        {
            etyArea eyArea = new etyArea();
            eyArea.CLIENT_ID = cboClient.Properties.KeyValue.ToString();
            eyArea.BUSS_TYPE = cboBussType.Properties.KeyValue.ToString();
            eyArea.AREA_NAME = tvArea.SelectedNode.Text;
            etyBaseTable eyBasTab = new etyBaseTable();
            eyBasTab = (etyBaseTable)tn.Tag;
            eyArea.SF = eyBasTab.X_CODE;
            eyArea.PORT_CODE = _basSysParam.PORT_CODE;
            //保存数据
            if (isInsert)
                bzArea.SaveArea(eyArea);
            else
                bzArea.DeleteArea(eyArea);
            //刷新数据
            ilAreaCurr = bzArea.GetArea(eyArea);
            ggcDocList.DataSource = ilAreaCurr;
        }

    }
}
